package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.PageQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public PageResult<User> page(PageQueryParam pageQueryParam) {
        //总条数
        long total = userMapper.findAll(pageQueryParam);
        //总页数
        Integer totalPage = (int) (total % pageQueryParam.getPageSize() == 0 ? total / pageQueryParam.getPageSize() : total / pageQueryParam.getPageSize() + 1);
        if (pageQueryParam.getPage() > totalPage) {
            pageQueryParam.setPage(totalPage);
        }
        if (pageQueryParam.getPage() < 1) {
            pageQueryParam.setPage(1);
        }
        List<User> userList = userMapper.page(pageQueryParam);
        return new PageResult<User>(total,pageQueryParam.getPage(),totalPage,userList);
    }

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }
}
